<?php
/**
 * @Author: zk
 * @Date:   18-5-10 下午5:20
 * @Description:    供应商平台询价相关
 */
class GYSQueryPrice
{
    
    /**
     * @Author: zk
     * @Date:   18-5-11 上午10:01
     * @Description:    供应商平台询价相关  [ 查询 经销商、康众 平台 报价和库存相关 ]
     * @param array $item_ids       erp 耗材id的数组
     * @param array $suppller_ids   erp 经销商的数组
     * @param int $shop_id          门店的id
     * @param int $format           返回数据格式类型 [ 默认值为1 ，1:普通数组格式; 2:供应商erpid为key,耗材erpid为key的多维关联数组; ]
     * @return array    
     *  array (size=5)
     *      'item_id' => '2070812       耗材erpid
     *      'suppller_id' => '4395      供应商erpid
     *      'type' => '2                类型 2 供应商
     *      'price' => '25200.000000'   含税报价 [单位 分]
     *      'storage' => 967            库存 [ -1 为无穷大]
     */
    public static function getQueryPriceList($item_ids, $suppller_ids, $shop_id, $format = 1){
        $res_arr = [];  //返回数组
        if(!is_array($item_ids) || !is_array($suppller_ids)){
            return $res_arr;
        }
        $method = 'scs/getPriceStorage';    //询价
        $token = 1;                         //token
        $item_ids = json_encode($item_ids);
        $suppller_ids = json_encode($suppller_ids);
        //$url = 'http://arcapi.anhui.com/scs/getPriceStorage?token=1&item_ids=[2070812,2084802,2084779,2084784]&suppller_ids=[4395,526]&shop_id=1';
//        $url = Config::get('config.arcapi_url').$method.'?token='.$token.'&item_ids='.$item_ids.'&suppller_ids='.$suppller_ids.'&shop_id='.$shop_id;
//        $response = Httpful::get($url)
//                ->timeoutIn(150)
//                ->sendsForm()
//                ->send();
        
        $request = array(
            'token' => $token,
            'item_ids' => $item_ids,
            'suppller_ids' => $suppller_ids,
            'shop_id' => $shop_id,
        );
        $response = Httpful::post(Config::get('config.arcapi_url').$method)
                ->timeoutIn(5000)
                ->body($request)
                ->sendsForm()->send();
        $response = json_decode($response);
        
        if($response->code == 0){
            switch ($format) {
                case 1:     //普通格式
                    foreach ($response->result->supply_info as $value) {
                        $temp_arr['item_id'] = $value->item_id;
                        $temp_arr['suppller_id'] = $value->suppller_id;
                        $temp_arr['type'] = $value->type;
                        $temp_arr['price'] = $value->price;
                        $temp_arr['storage'] = $value->storage;

                        $res_arr[] = $temp_arr;
                    }
                    
                    break;
                case 2:     //供应商erpid为key,耗材erpid为key的多维关联数组;
                    foreach ($response->result->supply_info as $value) {
                        $temp_arr['item_id'] = $value->item_id;
                        $temp_arr['suppller_id'] = $value->suppller_id;
                        $temp_arr['type'] = $value->type;
                        $temp_arr['price'] = $value->price;
                        $temp_arr['storage'] = $value->storage;

                        $res_arr[$value->suppller_id][$value->item_id] = $temp_arr;
                    }
                    
                    break;
                default:
                    foreach ($response->result->supply_info as $value) {
                        $temp_arr['item_id'] = $value->item_id;
                        $temp_arr['suppller_id'] = $value->suppller_id;
                        $temp_arr['type'] = $value->type;
                        $temp_arr['price'] = $value->price;
                        $temp_arr['storage'] = $value->storage;

                        $res_arr[] = $temp_arr;
                    }
                    
                    break;
            }
        }else{
            Log::error('res_error', [
                'class' => 'GYSQueryPrice',
                'function' => 'getQueryPriceList',
                'res_msg' => $response->msg,
                'item_ids' => $item_ids,
                'suppller_ids' => $suppller_ids,
            ]);
        }
        
        return $res_arr;
    }
    
    /**
     * @Author: zk
     * @Date:   18-5-14 下午4:00
     * @Description:    供应商平台下单
     * @param int $shop_id          门店的id    【相应的erpid】
     * @param int $suppller_id      供应商id    【相应的erpid】
     * @param int $pay_type         支付方式    【 0:现金; 1:定期(挂账)】
     * @param array $item_arr       耗材信息数组 【item_id 耗材id； amount 价格 单位 元； number 数量 为整数】
     *  $arr = [
            ['item_id' => '2070099', 'amount' => '76', 'number' => 1],
            ['item_id' => '2070099', 'amount' => '76', 'number' => 1],
        ];
     * @param object $address_info   收货人信息对象 【需要包含 name 收货人姓名； phone 收货人电话； address 收货人地址】
     * @return array 下单成功 返回供应商平台 下单单号等信息
     * $result = [
            'code' => '状态码', //0是成功，其他值为失败
            'msg' => '状态说明',
            'order' => '供应商平台订单号',  //下单失败则返回空
        ];
     */
    public static function createOrder($shop_id, $suppller_id, $pay_type, $item_arr, $address_info){
        //http://arcapi.anhui.com/scs/createOrder?token=1&item_ids=[{%22item_id%22:2070099,%22amount%22:76,%22number%22:1}]&suppller_id=4395&shop_id=1&pay_type=2&constant_name=lyt&constant_phone=123446&constant_address=%E4%B8%8B%E5%8D%95%E6%B5%8B%E8%AF%95%E5%9C%B0%E5%9D%80
        $method = 'scs/createOrder';    //下单
        $token = 1;                     //token
        $shop_id = $shop_id;            //门店id
        $suppller_id = $suppller_id;    //供应商id
        $item_ids = json_encode($item_arr);     //耗材信息数组
        //经销商平台支付方式  1:现金; 2:定期(挂账)
        //cms、门店平台支付方式  0:现金; 1:定期(挂账)
        switch ($pay_type) {
            case 0:
                $pay_type_gys = 1;
                break;
            case 1:
                $pay_type_gys = 2;
                break;
            default:
                $pay_type_gys = '';
                break;
        }  
        $constant_name = $address_info->name;       //收货人姓名
        $constant_phone = $address_info->phone;     //收货人电话
        $constant_address = $address_info->address; //收货人地址
        $request = array(
            'token' => $token,
            'item_ids' => $item_ids,
            'suppller_id' => $suppller_id,
            'shop_id' => $shop_id,
            'pay_type' => $pay_type_gys,
            'constant_name' => $constant_name,
            'constant_phone' => $constant_phone,
            'constant_address' => $constant_address,
        );
        $response = Httpful::post(Config::get('config.arcapi_url').$method)
                ->timeoutIn(5000)
                ->body($request)
                ->sendsForm()->send();
        $response = json_decode($response);
        
        if($response->code == 0){
            //返回数据数组
            $result = [
                'code' => $response->code,
                'msg' => $response->msg,
                'order' => $response->result->order_info,  //供应商平台订单号
            ];
            Log::info('createOrder', [
                'class' => 'GYSQueryPrice',
                'function' => 'createOrder',
                'res_msg' => $response->msg,
                'item_ids' => $item_ids,
                'suppller_id' => $suppller_id,
                'shop_id' => $shop_id,
                'pay_type' => $pay_type_gys,
                'constant_name' => $constant_name,
                'constant_phone' => $constant_phone,
                'constant_address' => $constant_address,
            ]);
        }else{
            //返回数据数组
            $result = [
                'code' => $response->code,
                'msg' => $response->msg,
                'order' => '',  //供应商平台订单号
            ];
            Log::error('res_error', [
                'class' => 'GYSQueryPrice',
                'function' => 'createOrder',
                'res_msg' => $response->msg,
                'item_ids' => $item_ids,
                'suppller_id' => $suppller_id,
                'shop_id' => $shop_id,
                'pay_type' => $pay_type_gys,
                'constant_name' => $constant_name,
                'constant_phone' => $constant_phone,
                'constant_address' => $constant_address,
            ]);
        }
        
        return $result;
    }

    /**
     * @Author: zk
     * @Date:   18-5-15 下午1:06
     * @Description:    收货之后更改订单状态
     * @param string $order_id       供应商平台订单id
     * @return string 更改订单状态成功 返回供应商平台 下单单号， 否则返回 '' 空
     */
    public static function changeOrder($order_id){
        $method = 'scs/changeOrder';        //更改订单状态
        $token = 1;                         //token
        //http://arcapi.anhui.com/scs/changeOrder?token=1&order_id=3624076377
        
        $request = array(
            'token' => $token,
            'order_id' => $order_id,
        );
        $response = Httpful::post(Config::get('config.arcapi_url').$method)
                ->timeoutIn(5000)
                ->body($request)
                ->sendsForm()->send();
        $response = json_decode($response);
        
        $order = '';  //供应商平台订单号
        if($response->code == 0){
            $order = $response->result->order_info;
            Log::info('changeOrder_info', [
                'class' => 'GYSQueryPrice',
                'function' => 'changeOrder',
                'res_msg' => $response->msg,
                'order_id' => $order_id,
            ]);
        }else{
            Log::error('res_error', [
                'class' => 'GYSQueryPrice',
                'function' => 'changeOrder',
                'res_msg' => $response->msg,
                'order_id' => $order_id,
            ]);
        }
        
        return $order;
    }
    
    /**
     * @Author: zk
     * @Date:   18-6-8 下午3:01
     * @Description:    根据车架号查询车架号信息及适配耗材
     * @param string $vin   车架号
     * @return array 耗材的erp_id 数组
     */
    public static function getVinInfo($vin){
        //http://arcapi.anhui.com/scs/getVin?token=1&vin=LBV3B1405EMB32307
        $method = 'scs/getVin';             //获取车架号信息 地址
        $token = 1;                         //token
        $request = array(
            'token' => $token,
            'vin' => $vin,
        );
        $response = Httpful::post(Config::get('config.arcapi_url').$method)
                ->timeoutIn(5000)
                ->body($request)
                ->sendsForm()->send();
        $response = json_decode($response, TRUE);
        
        $res_arr = [];
        if($response['code'] == 0 && !empty($response)){
            $res_arr = [
                'item_erp_id' => $response['result']['item_info'],      //耗材的erp_id数组
                'vin_info' => [
                    'brandName' => isset($response['result']['car_info']['brandName'])?$response['result']['car_info']['brandName']:'',    //品牌
                    'vehicleSystem' => isset($response['result']['car_info']['vehicleSystem'])?$response['result']['car_info']['vehicleSystem']:'',    //车系
                    'year' => isset($response['result']['car_info']['year'])?$response['result']['car_info']['year']:'',      //年款
                    'vehicleTypeName' => isset($response['result']['car_info']['vehicleTypeName'])?$response['result']['car_info']['vehicleTypeName']:'',    //车型名称
                ],
            ];
            
            $insert_res = DataCarVinInfo::setupVinInfo($vin, $response['result']['car_info']);     //保存车架号相应信息
            if($insert_res){
                Log::info('getVinInfo', [
                    'class' => 'GYSQueryPrice',
                    'function' => 'getVinInfo',
                    'msg' => 'DataCarVinInfo::setupVinInfo(), insert ok',
                    'vin' => $vin,
                ]);
            } else {
                Log::error('insert_error', [
                    'class' => 'GYSQueryPrice',
                    'function' => 'getVinInfo',
                    'msg' => 'DataCarVinInfo::setupVinInfo(), insert error',
                    'vin' => $vin,
                ]);
            }
        }else{
            Log::error('res_error', [
                'class' => 'GYSQueryPrice',
                'function' => 'getVinInfo',
                'res_msg' => $response['msg'],
                'vin' => $vin,
            ]);
        }
        
        return $res_arr;
    }
    
}